<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRole" @close="closeFn" @open="openRoleFn">
    <!-- 这里准备复选框 -->
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="comfireFn">确定</el-button>
      <el-button size="small" @click="closeFn()">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListAPI, assignRolesAPI } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRole: {
      type: Boolean,
      default: false
    },
    curId: { // 当前id
      type: String,
      default: ''
    }
  },

  data() {
    return {
      checkList: [], // 选择角色列表
      roleList: [] // 全部角色列表
    }
  },
  methods: {
    closeFn() {
      this.$emit('close-role')
    },
    // 打开弹框前获取数据
    openRoleFn() {
      this.getRoleList()
      // console.log('角色列表', res)
      this.getRoleDetail()
    },
    async getRoleList() {
      const res = await getRoleListAPI({ page: 1, pageSize: 100 })
      // console.log(res)
      this.roleList = res.data.rows
    },

    // 确定按钮
    async comfireFn() {
      await assignRolesAPI({ id: this.curId, roleIds: this.checkList })
      this.$message.success('分配角色成功')
      this.closeFn()
    },
    // 获取详情角色列表
    async  getRoleDetail() {
      const res = await getUserDetailById(this.curId)
      console.log(res)
      this.checkList = res.data.roleIds
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
